package leetcode.hot100;

public class Solution647 {
    public int countSubstrings(String s) {
        //动态规划，dp[i][j]表示s[i,j]是否回文
        //dp[i,j] = dp[i+1,j-1]&&(s[i]==s[j])
        int n = s.length(), count = 0;
        boolean[][] dp = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            dp[i][i] = true;
            count++;
        }
        for (int i = 0; i < n-1; i++) {
            dp[i][i+1] = s.charAt(i)==s.charAt(i+1);
            if(dp[i][i+1]) count++;
        }
        for (int i = n-2; i >= 0 ; i--) {
            for (int j = i+2; j < n; j++) {
                dp[i][j] = (s.charAt(i)==s.charAt(j))&&dp[i+1][j-1];
                if(dp[i][j]) count++;
            }
        }
        return count;
    }
}
